]> git.r.bdr.sh - rbdr/super-polarity/blame - Super Polarity/ActorManager.cs
I have the worst commits ever.
[rbdr/super-polarity] / Super Polarity / ActorManager.cs
CommitLineData
f8aec187
BB
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Xna.Framework;
6using Microsoft.Xna.Framework.Graphics;
7
8namespace SuperPolarity
9{
10 static class ActorManager
11 {
38c7d3f9 12
74c15570 13 static SuperPolarity Game;
38c7d3f9 14 static int OutlierBounds;
74c15570 15 static IList<Actor> Actors;
f8aec187
BB
16
17 static ActorManager()
18 {
38c7d3f9 19 OutlierBounds = 100;
f8aec187
BB
20 Actors = new List<Actor>();
21 }
22
23 static public void CheckIn(Actor actor)
24 {
25 Actors.Add(actor);
26 }
27
28 static public void CheckOut(Actor actor)
29 {
30 Actors.Remove(actor);
31 }
32
33 static public void Update(GameTime gameTime)
34 {
2af83e98 35 CheckActors();
38c7d3f9 36 CheckOutliers();
f8aec187
BB
37 foreach (Actor actor in Actors)
38 {
39 actor.Update(gameTime);
40 }
41 }
42
43 static public void Draw(SpriteBatch spriteBatch)
44 {
45 foreach (Actor actor in Actors)
46 {
47 actor.Draw(spriteBatch);
48 }
49 }
2af83e98
BB
50
51 static void CheckActors()
52 {
74c15570 53 for (var i = Actors.Count - 1; i >= 0; i--)
2af83e98 54 {
74c15570
BB
55 if (i >= Actors.Count) {
56 i = Actors.Count - 1;
57 }
58 Actor actor = Actors[i];
59 for (var j = i - 1; j >= 0; j--)
2af83e98 60 {
74c15570
BB
61 Actor other = Actors[j];
62
2af83e98
BB
63 CheckCollision(actor, other);
64
65 if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
66 {
67 CheckMagnetism((Ship)actor, (Ship)other);
68 }
69 }
70 }
71 }
72
73 static void CheckCollision(Actor actor, Actor other)
74 {
74c15570
BB
75 var collision = Rectangle.Intersect(actor.Box, other.Box);
76 var inverseCollision = Rectangle.Intersect(other.Box, actor.Box);
77 if (!collision.IsEmpty && !actor.Dying && !other.Dying)
78 {
79 actor.Collide(other, collision);
80 other.Collide(actor, inverseCollision);
81 }
2af83e98
BB
82
83 }
84
85 static void CheckMagnetism(Ship actor, Ship other)
86 {
87 if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
88 {
89 var dy = other.Position.Y - actor.Position.Y;
90 var dx = other.Position.X - actor.Position.X;
91 var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
92 var angle = (float) Math.Atan2(dy, dx);
38c7d3f9 93 var otherAngle = (float)Math.Atan2(-dy, -dx);
2af83e98
BB
94
95 if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
96 {
97 actor.Magnetize(other, (float)linearDistance, angle);
38c7d3f9 98 other.Magnetize(actor, (float)linearDistance, otherAngle);
2af83e98
BB
99 }
100 }
101 }
38c7d3f9
BB
102
103 static void CheckOutliers()
104 {
105 for (var i = Actors.Count; i > 0; i--)
106 {
107 var actor = Actors[i-1];
108 if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds ||
109 actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds ||
110 actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds)
111 {
112 CheckOut(actor);
113 }
114 }
115 }
116
74c15570 117 internal static void SetGame(SuperPolarity game)
38c7d3f9
BB
118 {
119 Game = game;
120 }
f8aec187
BB
121 }
122}